/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entities;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;



/*
 * nie moge nazwać tego 'Order', bo to ponoć jakies słowo kluczowe w JPA
 */
@Entity
@Table(name="ORDERS")
@NamedQueries({
    @NamedQuery(name = "getAllOrders",
    query = "SELECT o FROM Orderr o"),
    @NamedQuery(name = "removeOrderForId",
    query = "DELETE FROM Orderr a WHERE a.id = :orderID")
})
public class Orderr implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "ORDERS_BOOKS", joinColumns = {
        @JoinColumn(name = "ORDER_ID")
    }, inverseJoinColumns = {
        @JoinColumn(name = "BOOK_ID")
    })
    private Collection<Book> books;

    @JoinColumn(name = "CLIENT_ID")
    @ManyToOne(cascade = CascadeType.ALL)
    private Client client;

    public Orderr() {
        books = new ArrayList<Book>();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Collection<Book> getBooks() {
        return books;
    }

    public Client getClient() {
        return client;
    }

    public void setBooks(Collection<Book> books) {
        this.books = books;
    }

    public void setClient(Client client) {
        this.client = client;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Orderr)) {
            return false;
        }
        Orderr other = (Orderr) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Orderr[id=" + id + "]";
    }
    public double getTotalValue()
    {
        double totalValue=0;
        for(Book book: books)
            totalValue += book.getPrice();

        BigDecimal bd = new BigDecimal(totalValue);
        bd = bd.setScale(2,BigDecimal.ROUND_UP);
        //bd
        return bd.doubleValue();
    }
    public String getBooksAsString()
    {
        String out = "";
        for(Book book: books)
            out += book.getTitle() + ", ";
        if(out.length() > 0)
            out = out.substring(0, out.length()-2);
        return out;
    }

}
